<?php
namespace app\home\controller;
use think\Controller;
use think\Db;
use think\Request;
use app\home\controller\Common;
use think\Session;


class Shop extends Controller
{
    public function index()
    {
//        $id= Session::get('user_id');
//        $sql="select * from user  WHERE user_id=$id";
//        $data=Db::query($sql);
        //店铺信息
        $id  = Request::instance();
        $id  = $id->get('merchant_id');
        $sql = "select * from shop_merchant  WHERE merchant_id=$id";
        $res = Db::query($sql);
        //轮播图
        $sql = "select * from shop_ad inner join shop_merchant on shop_ad.ad_person=shop_merchant.merchant_id WHERE merchant_id=$id  AND is_show=1";
        $tu  = Db::query($sql);

        // var_dump($tu);die;
        //推荐商品
        $sql = "select * from shop_goods inner join shop_merchant on shop_goods.goods_seller_id = shop_merchant.merchant_id WHERE merchant_id=$id  AND goods_reco=1 limit 8";
        $tui = Db::query($sql);
        foreach ($tui as $key => $val) {
            $sql = "select * from goods_img where  img_gid = " . $val['goods_id'] . " limit 1";
            $sho = Db::query($sql);
            if (empty($sho[0]['img_path'])) {
                $tui[$key]['goods_img'] = 'uploads/timg.jpg';
            } else {
                $tui[$key]['goods_img'] = $sho[0]['img_path'];
            }

        }

        //上新商品
        $sql = "select * from shop_goods inner join shop_merchant on shop_goods.goods_seller_id = shop_merchant.merchant_id WHERE merchant_id=$id  AND goods_new=1 limit 8";
        $new = Db::query($sql);
        foreach ($new as $key => $val) {
            $sql = "select * from goods_img where  img_gid = " . $val['goods_id'] . " limit 1";
            $sho = Db::query($sql);
            if (empty($sho[0]['img_path'])) {
                $new[$key]['goods_img'] = 'uploads/timg.jpg';
//                var_dump($new[$key]['goods_img']);die;
            } else {
                $new[$key]['goods_img'] = $sho[0]['img_path'];
            }
        }
            // var_dump($tui);die;
            return $this->fetch('Shop/jinrudianpu', ['res' => $res, 'tu' => $tu, 'tui' => $tui, 'new' => $new,'merchant_id' => $id]);
    }
    //销量
    public function liang(){
        
            $res=Request::instance();
            $id=$res->get('merchant_id');
            $sou=$res->get('sou');
            $where = "1 = 1";
            //        halt($sou);
            if ($sou) {
                $where .= " and g.goods_name like '%$sou%' ";
            }
            //var_dump($where);die;
            $sql = "select * from shop_merchant WHERE merchant_id= $id";
            $res = Db::query($sql);
            //店铺商品
            $sql = "select * from shop_goods as g inner join shop_merchant as m on g.goods_seller_id = m.merchant_id WHERE $where and m.merchant_id=$id";
             $data = Db::query($sql);
            foreach ($data as $key => $val) {
                $sql = "select * from goods_img where  img_gid = " .$val['goods_id']. " limit 1";
                $sho = Db::query($sql);
                if (empty($sho[0]['img_path'])) {
                    $data[$key]['goods_img'] = 'uploads/timg.jpg';
    //                var_dump($new[$key]['goods_img']);die;
                } else {
                    $data[$key]['goods_img'] = $sho[0]['img_path'];
                }
            }
            if(Request::instance()->isAjax()){
                $xiao = $_GET['xiao'];
               // $xui = $_GET['xui'];
                if($xiao == 2){
                    $sql="select * from shop_goods as g inner join shop_merchant as m on g.goods_seller_id = m.merchant_id WHERE $where and m.merchant_id=$id order by goods_num desc";
                    $xq = Db::query($sql);
                    $xiao = 3;
                }else if($xiao == 3){
                    $sql="select * from shop_goods as g inner join shop_merchant as m on g.goods_seller_id = m.merchant_id WHERE $where and m.merchant_id=$id order by goods_num asc";
                    $xq = Db::query($sql);
                    $xiao = 2;
                }
                echo json_encode(array('data'=>$xq,'xiao'=>$xiao));
                die;
            }else{
              //店铺id
            //        halt($data);
           return $this->fetch('Shop/jinrudianpuxiangqing', ['res' => $res,'data'=>$data,'id'=>$id,'sou'=>$sou]);
          }   
       //   var_dump($xq);die;        
    }
    //时间
    public function dianpu(){
        
            $res=Request::instance();
            $id=$res->get('merchant_id');
            $sou=$res->get('sou');
            $where = "1 = 1";
            //        halt($sou);
            if ($sou) {
                $where .= " and g.goods_name like '%$sou%' ";
            }
            //var_dump($where);die;
            $sql = "select * from shop_merchant WHERE merchant_id= $id";
            $res = Db::query($sql);
            //店铺商品
            $sql = "select * from shop_goods as g inner join shop_merchant as m on g.goods_seller_id = m.merchant_id WHERE $where and m.merchant_id=$id";
             $data = Db::query($sql);
            foreach ($data as $key => $val) {
                $sql = "select * from goods_img where  img_gid = " .$val['goods_id']. " limit 1";
                $sho = Db::query($sql);
                if (empty($sho[0]['img_path'])) {
                    $data[$key]['goods_img'] = 'uploads/timg.jpg';
    //                var_dump($new[$key]['goods_img']);die;
                } else {
                    $data[$key]['goods_img'] = $sho[0]['img_path'];
                }
            }
            if(Request::instance()->isAjax()){
                $gai = $_GET['gai'];
               // $xui = $_GET['xui'];
                if($gai == 2){
                    $sql="select * from shop_goods as g inner join shop_merchant as m on g.goods_seller_id = m.merchant_id WHERE $where and m.merchant_id=$id order by goods_uptime desc";
                    $xq = Db::query($sql);
                    $gai = 3;
                }else if($gai == 3){
                    $sql="select * from shop_goods as g inner join shop_merchant as m on g.goods_seller_id = m.merchant_id WHERE $where and m.merchant_id=$id order by goods_uptime asc";
                    $xq = Db::query($sql);
                    $gai = 2;
                }
                echo json_encode(array('data'=>$xq,'gai'=>$gai));
                die;
            }else{
              //店铺id
            //        halt($data);
           return $this->fetch('Shop/jinrudianpuxiangqing', ['res' => $res,'data'=>$data,'id'=>$id,'sou'=>$sou]);
          }   
       //   var_dump($xq);die;        
    }
    //价格
    public function xin(){
        
            $res=Request::instance();
            $id=$res->get('merchant_id');
            $sou=$res->get('sou');
            $where = "1 = 1";
            //        halt($sou);
            if ($sou) {
                $where .= " and g.goods_name like '%$sou%' ";
            }
            //var_dump($where);die;
            $sql = "select * from shop_merchant WHERE merchant_id= $id";
            $res = Db::query($sql);
            //店铺商品
            $sql = "select * from shop_goods as g inner join shop_merchant as m on g.goods_seller_id = m.merchant_id WHERE $where and m.merchant_id=$id";
             $data = Db::query($sql);
            foreach ($data as $key => $val) {
                $sql = "select * from goods_img where  img_gid = " .$val['goods_id']. " limit 1";
                $sho = Db::query($sql);
                if (empty($sho[0]['img_path'])) {
                    $data[$key]['goods_img'] = 'uploads/timg.jpg';
    //                var_dump($new[$key]['goods_img']);die;
                } else {
                    $data[$key]['goods_img'] = $sho[0]['img_path'];
                }
            }
            if(Request::instance()->isAjax()){
                // $gai = $_GET['gai'];
               $xui = $_GET['xui'];
                 //if传过来的是2 sql语句执行完 吧2 改成3
                if($xui == 2){
                    $sql="select * from shop_goods as g inner join shop_merchant as m on g.goods_seller_id = m.merchant_id WHERE $where and m.merchant_id=$id order by goods_price desc";
                    $xq = Db::query($sql);
                    $xui = 3;
                 //if传过来的是3 sql语句执行完 吧3 改成2
                }else if($xui == 3){
                    $sql="select * from shop_goods as g inner join shop_merchant as m on g.goods_seller_id = m.merchant_id WHERE $where and m.merchant_id=$id order by goods_price asc";
                    $xq = Db::query($sql);
                    $xui = 2;
                }
                echo json_encode(array('data'=>$xq,'xui'=>$xui));
                die;
            }else{
              //店铺id
            //        halt($data);
           return $this->fetch('Shop/jinrudianpuxiangqing', ['res' => $res,'data'=>$data,'id'=>$id,'sou'=>$sou]);
          }
            
       //   var_dump($xq);die;
            
    }
}